﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using SpringWaterDataAccess;

namespace sow
{
	/// <summary>
	/// Summary description for jianzao.
	/// </summary>
	public partial class denglu : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.Button button;
		protected System.Web.UI.WebControls.TextBox email;
		protected System.Web.UI.WebControls.TextBox password;
		protected System.Web.UI.WebControls.ValidationSummary valSum;
	
		protected void Page_Load(object sender, System.EventArgs e)
		{
			// Put user code to initialize the page here
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
			base.OnLoad(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.CVdatabase.ServerValidate += new System.Web.UI.WebControls.ServerValidateEventHandler(this.CVdatabase_ServerValidate);

		}
		#endregion

		protected void Button1_Click(object sender, System.EventArgs e)
		{
				UserObject u = 
				   UserObject.LoginOn(idEmail.Value,idPassword.Value);
				if (null != u) // login succeed
				{
					HttpCookie roleCookie = new HttpCookie("Roles", u.RoleName);
					Response.Cookies.Add(roleCookie);
					HttpCookie userNickNameCookie = new HttpCookie("NickName", u.NickName);
					Response.Cookies.Add(userNickNameCookie);
					FormsAuthentication.RedirectFromLoginPage(idEmail.Value,true);
					Response.Redirect("index.aspx");
				}
				else
				{
					// stay on the same page and display login fail error
				}
		}

		protected void idPassword_ServerChange(object sender, System.EventArgs e)
		{
		
		}

		private void CVdatabase_ServerValidate(object source, System.Web.UI.WebControls.ServerValidateEventArgs args)
		{
			if(Page.IsValid)
			{
				try
				{
					UserObject u= UserObject.LoginOn(idEmail.Value,idPassword.Value);
				}
				catch(Exception e)
				{
					args.IsValid = false; //this will cause the error to show
				}
			}
		}

		protected void idEmail_ServerChange(object sender, System.EventArgs e)
		{
		
		}
	}
}
